package com.fan.push.mi;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;

import com.fan.base.util.Logger;
import com.xiaomi.mipush.sdk.ErrorCode;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;

import java.util.List;
import java.util.Objects;

/**
 * thirdParty
 *
 * @author fan
 * @date Created in 2019-09-23
 */
public class MiPushMessageReceiver extends PushMessageReceiver {

    private String mRegId;

    private String mTopic;

    private String mAlias;

    private String mAccount;

    private String mStartTime;

    private String mEndTime;

    /**
     * 用来接收服务器向客户端发送的透传消息
     *
     * @param context context
     * @param message message
     */
    @Override
    public void onReceivePassThroughMessage ( Context context, MiPushMessage message ) {

        Logger.e( "onReceivePassThroughMessage is called. " + message.toString() );
        if ( !TextUtils.isEmpty( message.getTopic() ) ) {
            mTopic = message.getTopic();
            Logger.e( "onReceivePassThroughMessage:mTopic::" + mTopic );
        } else if ( !TextUtils.isEmpty( message.getAlias() ) ) {
            mAlias = message.getAlias();
            Logger.e( "onReceivePassThroughMessage:mAlias::" + mAlias );
        }

    }

    /**
     * 接收服务器向客户端发送的通知消息
     *
     * @param context context
     * @param message message
     */
    @Override
    public void onNotificationMessageClicked ( Context context, MiPushMessage message ) {

        Logger.e( "onNotificationMessageClicked is called. " + message.toString() );

        if ( !TextUtils.isEmpty( message.getTopic() ) ) {
            mTopic = message.getTopic();
            Logger.e( "onNotificationMessageClicked:mTopic::" + mTopic );
        } else if ( !TextUtils.isEmpty( message.getAlias() ) ) {
            mAlias = message.getAlias();
            Logger.e( "onNotificationMessageClicked:mAlias::" + mAlias );
        }
    }

    /**
     * 接收服务器向客户端发送的通知消息
     * 通知消息到达客户端时触发。另外应用在前台时不弹出通知的通知消息到达客户端也会触发这个回调函数。
     *
     * @param context context
     * @param message message
     */
    @Override
    public void onNotificationMessageArrived ( Context context, MiPushMessage message ) {

        Logger.e( "onNotificationMessageArrived is called. " + message.toString() );

        if ( !TextUtils.isEmpty( message.getTopic() ) ) {
            mTopic = message.getTopic();
            Logger.e( "onNotificationMessageArrived:mTopic::" + mTopic );
        } else if ( !TextUtils.isEmpty( message.getAlias() ) ) {
            mAlias = message.getAlias();
            Logger.e( "onNotificationMessageArrived:mAlias::" + mAlias );
        }
    }

    /**
     * 接收客户端向服务器发送命令后的响应结果
     *
     * @param context context
     * @param message message
     */
    @Override
    public void onCommandResult ( Context context, MiPushCommandMessage message ) {

        Logger.e( "onCommandResult is called. " + message.toString() );

        String command = message.getCommand();
        List< String > arguments = message.getCommandArguments();
        String cmdArg1 = ( ( arguments != null && arguments.size() > 0 ) ? arguments.get( 0 ) : null );
        String cmdArg2 = ( ( arguments != null && arguments.size() > 1 ) ? arguments.get( 1 ) : null );
        if ( MiPushClient.COMMAND_REGISTER.equals( command ) ) {
            if ( message.getResultCode() == ErrorCode.SUCCESS ) {
                mRegId = cmdArg1;
                Logger.e( "COMMAND_REGISTER:mRegId::" + mRegId );
            } else {
                Logger.e( "COMMAND_REGISTER:register_fail" );
            }
        } else if ( MiPushClient.COMMAND_SET_ALIAS.equals( command ) ) {
            if ( message.getResultCode() == ErrorCode.SUCCESS ) {
                mAlias = cmdArg1;
                Logger.e( "COMMAND_SET_ALIAS:mAlias::" + mAlias );
            } else {
                Logger.e( "COMMAND_SET_ALIAS:alias_fail" );
            }
        } else if ( MiPushClient.COMMAND_UNSET_ALIAS.equals( command ) ) {
            if ( message.getResultCode() == ErrorCode.SUCCESS ) {
                mAlias = cmdArg1;
                Logger.e( "COMMAND_UNSET_ALIAS:" + mAlias );
            } else {
                Logger.e( "COMMAND_UNSET_ALIAS:unset_alias_fail" );
            }
        } else if ( MiPushClient.COMMAND_SET_ACCOUNT.equals( command ) ) {
            if ( message.getResultCode() == ErrorCode.SUCCESS ) {
                mAccount = cmdArg1;
                Logger.e( "COMMAND_SET_ACCOUNT:" + mAccount );
            } else {
                Logger.e( "COMMAND_UNSET_ALIAS:set_account_fail" );
            }
        } else if ( MiPushClient.COMMAND_UNSET_ACCOUNT.equals( command ) ) {
            if ( message.getResultCode() == ErrorCode.SUCCESS ) {
                mAccount = cmdArg1;
                Logger.e( "COMMAND_UNSET_ACCOUNT:" + mAccount );
            } else {
                Logger.e( "COMMAND_UNSET_ACCOUNT:unset_account_fail" );
            }
        } else if ( MiPushClient.COMMAND_SUBSCRIBE_TOPIC.equals( command ) ) {
            if ( message.getResultCode() == ErrorCode.SUCCESS ) {
                mTopic = cmdArg1;
                Logger.e( "COMMAND_SUBSCRIBE_TOPIC:" + mTopic );
            } else {
                Logger.e( "COMMAND_SUBSCRIBE_TOPIC:subscribe_topic_fail" );
            }
        } else if ( MiPushClient.COMMAND_UNSUBSCRIBE_TOPIC.equals( command ) ) {
            if ( message.getResultCode() == ErrorCode.SUCCESS ) {
                mTopic = cmdArg1;
                Logger.e( "COMMAND_SUBSCRIBE_TOPIC:" + mTopic );
            } else {
                Logger.e( "COMMAND_SUBSCRIBE_TOPIC:unsubscribe_topic_fail" );
            }
        } else if ( MiPushClient.COMMAND_SET_ACCEPT_TIME.equals( command ) ) {
            if ( message.getResultCode() == ErrorCode.SUCCESS ) {
                mStartTime = cmdArg1;
                mEndTime = cmdArg2;
                Logger.e( "COMMAND_SET_ACCEPT_TIME:" + mStartTime + "~mEndTime:" + mEndTime );
            } else {
                Logger.e( "COMMAND_SET_ACCEPT_TIME:set_accept_time_fail:" + message.getReason() );
            }
        } else {
            Logger.e( "onCommandResult default:" + message.getReason() );
        }
    }

    /**
     * 接收客户端向服务器发送注册命令后的响应结果
     *
     * @param context context
     * @param message message
     */
    @Override
    public void onReceiveRegisterResult ( Context context, MiPushCommandMessage message ) {

        Logger.e( "onReceiveRegisterResult is called. " + message.toString() );

        String command = message.getCommand();
        List< String > arguments = message.getCommandArguments();
        String cmdArg1 = ( ( arguments != null && arguments.size() > 0 ) ? arguments.get( 0 ) : null );
        if ( MiPushClient.COMMAND_REGISTER.equals( command ) ) {
            if ( message.getResultCode() == ErrorCode.SUCCESS ) {
                mRegId = cmdArg1;
                Logger.e( "COMMAND_REGISTER:mRegId：：" + mRegId );
            } else {
                Logger.e( "COMMAND_REGISTER:register_fail" );
            }
        } else {
            Logger.e( "onReceiveRegisterResult default:" + message.getReason() );
        }
    }

    @Override
    public void onRequirePermissions ( Context context, String[] permissions ) {
        super.onRequirePermissions( context, permissions );

        Logger.e( "onRequirePermissions is called. " + arrayToString( permissions ) );

        if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.M ) {
            Intent intent = new Intent();
            intent.putExtra( "permissions", permissions );
            intent.setComponent( new ComponentName( context.getPackageName(), Objects.requireNonNull( PermissionActivity.class.getCanonicalName() ) ) );
            intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS );
            context.startActivity( intent );
        }
    }

    public String arrayToString ( String[] strings ) {
        StringBuilder result = new StringBuilder( " " );
        for ( String str : strings ) {
            result.append( str ).append( " " );
        }
        return result.toString();
    }
}
